/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-05 10:38:26
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-05 10:42:57
 * @Description: file content
 */

#include "apue.h"
#include <sys/wait.h>

int main()
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
        err_sys("first fork error");
    }
    else if (pid == 0)
    {
        if ((pid = fork()) < 0)
        {
            err_sys("second fork error");
        }
        else if (pid > 0)
        {
            exit(0);
        }

        sleep(2);
        printf("second child, parent pid = %ld\n", (long)getppid());
        exit(0);
    }

    if (waitpid(pid, NULL, 0) != pid)
    {
        err_sys("waitpid error");
    }

    exit(0);
}
